/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package threads;

import filters.ByteStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import own_packets.Protocol;
import pojo.Files;

/**
 *
 * @author Hnmtuan
 */
public class Proccess implements Runnable {

    private Socket _ownSocket;
    private OutputStream os;
    private InputStream _in;
    private ObjectOutputStream _objOutput;
    private String _messages;

    public Proccess(Socket socket) {
        _ownSocket = socket;
    }

    public void run() {
        try {

            // Create inputStream to received data from a following server
            _in = _ownSocket.getInputStream();

            // Received Response from a following server
            Protocol objRS = ByteStream.toProtocol(_in);

            if (objRS == null) {
                System.out.println("Received Null Object From Server");
                return;
            }

            if (objRS.getRequestType() == Protocol.RequestType.SEARCH_REQUEST) {
                _messages = objRS.getMessages();
                System.out.println(String.format("Search File from client : %s", _messages));
                os = _ownSocket.getOutputStream();

                if (os == null) {
                    System.out.println("Failed in send files !");
                    return;
                }

                if (_messages == null) {
                    System.out.println("Messages is null !");
                    return;
                }
                
                List<Files> files = dao.Files.getFilesByNames(_messages);

                Protocol myPacket = new Protocol();
                myPacket.setIsRequest(Boolean.FALSE);
                myPacket.setMessages("Response test file");
                myPacket.setFiles(files);
                
                ByteStream.toStream(os, myPacket);

                System.out.println("Send to client success ");
            } else {
                // Output result to layout
                System.out.println(String.format("Messages from client : %s", objRS.getMessages()));
                os = _ownSocket.getOutputStream();

                if (os == null) {
                    System.out.println("Failed in send files !");
                    return;
                }

                Protocol myPacket = new Protocol();
                myPacket.setIsRequest(Boolean.FALSE);
                myPacket.setMessages("Response test file");

                ByteStream.toStream(os, myPacket);

                System.out.println("Send to client success ");
            }


            _ownSocket.close();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Proccess.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            System.out.println(String.format("Failed in send to client : %s ", ex.getMessage()));
        }

    }
}
